<?php
	Class verifylogin extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('usuario','',true);
	}

	function index(){

		$this->form_validation->set_rules('mail','Email','required');
		$this->form_validation->set_rules('pass','Password','required|callback_checkdb');

		if ($this->form_validation->run() == false) {
			$error= validation_errors();
			$data['error'] = $error;
			$this->load->view('login-form',$data);
		}else{
			redirect('administrador');
		}
	}	

	function checkdb($pass){
		$this->load->model('usuario_model');
		$mail = $this->input->post('mail');

	    $result = $this->usuario->login($mail, $pass);
	    if($result)
	    {
	      $sess_array = array();
	      foreach($result as $row)
	      {
	        $sess_array = array(
	          'id' => $row->id_usuario,
	          'imagen' => $row->imagen,
	          'nombre' => $row->nombre,
	          'apellido' => $row->apellido,
	          'email' => $row->email,
	          'tipo_user' => $row->tipo,
	          'id_sede' => $row->id_sede
	        );

	       $this->session->set_userdata('logged_in', $sess_array);
	      }
	      return TRUE;
	    }
	    else{
	    	$this->form_validation->set_message('checkdb', 'Invalid username or password');
     		return false;
   		}
	}
}
	 
?>